import {
    editView
} from './views/edit.view';
import {
    validate
} from './validate';
import {
    getCateList
} from './models/getCatelist.model';
import {
    submit
} from './submit';
import {
    msg, modal6
} from '../common/layer';

import {
    uploadMuchImg
} from '../common/uploadImg';

const editBtnHandler = () => {

    let selectedList = $('#table').bootstrapTable('getSelections');
    if (selectedList.length === 0) {
        msg('请选择一项编辑');
        return;
    }

    const localData = selectedList[0];

    modal6('编辑', editView(localData));

    $(".modal-form input").iCheck({
        checkboxClass: 'icheckbox_minimal-blue',
        radioClass: 'iradio_minimal-blue'
    });

    $('#pro').on('ifChecked', function (e) {
        $("#equipment").hide();
        $("#product").show();
        $(".explain").show();
    });
    $('#equip').on('ifChecked', function (e) {
        $(".explain").hide();
        $("#product").hide();
        $("#equipment").show();
    });

    if(localData.type == 2){
        $(".explain").hide();
        $("#product").hide();
        $("#equipment").show();

    }else{
        $("#equipment").hide();
        $("#product").show();
        $(".explain").show();
    }

    uploadMuchImg('#picker3', '#thumb'); // 缩略图
    uploadMuchImg('#picker4', '#enclosure'); // 附件

    var imgArr1 = localData.thumb.split(",");
    $.each(imgArr1, function (key, val) {
        $("#picker3").siblings('.list').append('<div class="file-item thumbnail" >' + '<img src="' + val + '" class="editImg">' + '</div>');
    })
    $("#picker3").siblings('#thumb').val(localData.thumb);

    var imgArr2 = localData.enclosure.split(",");
    $.each(imgArr2, function (key, val) {
        $("#picker4").siblings('.list').append('<div class="file-item thumbnail">' + '<img src="' + val + '" class="editImg">' + '</div>');
    })
    $("#picker4").siblings('#enclosure').val(localData.enclosure);


    $(document).on('click','#tabbutton li',function(){
        let index = $(this).index();
        if(index == 1){
            $('.baseInfo').removeClass('active').addClass('shownone');
            $('.detail').removeClass('shownone').addClass('active');
            $(this).addClass('hover').siblings('li').removeClass('hover');
        }else{
            $('.detail').removeClass('active').addClass('shownone');
            $('.baseInfo').removeClass('shownone').addClass('active');
            $(this).addClass('hover').siblings('li').removeClass('hover');
        }
    });

    getCateList(localData.cid);

    $('#submitBtn').click(() => {
        let data = {};
        $.each($('#form').serializeArray(), (key, val) => {
            data[val.name] = val.value;
        });
        if (!validate(data)) {
            return;
        }
        submit(GLOBAL_proAddUrl, data);
    });


};


export {
    editBtnHandler
}